%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Yifan Zhang
% Bejing University of Technology
% Copy Right 2023
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find local maxima circular
% 找到局部最大的交叉验证值
%
% Find the local maxima circular_cross_correlation, to 
% calculate the best rotate.
%
% INPUT: 
%   seq: The result of function circular_cross_correlation.
%
% OUTPUT
%   maxima: max element in seq.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function maxima = find_local_maxima_circular(seq)

% Check if the input sequence is not empty
assert(~isempty(seq), 'Input sequence must not be empty.');

s = length(seq);
ex = [seq(s), seq, seq(1)];
m = ((ex(2:end-1) > ex(1:end-2)) & (ex(2:end-1) > ex(3:end)))';

maxima = 0;
for i = 1:s
    if m(i)
        maxima = i-1;
        break;
    end
end

if maxima == 0
    error('No local maxima found.');
end

end